﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using YmtAuth.Domain.Model.User;
using YmtAuth.Domain.Repository.MsSql;
using YmtAuth.Repository.DapperRepository;

namespace YmtAuth.Repository.MSSQL
{
    public class UserRepository : _Repository<YmtUser>, IUserRepository
    {
        public UserRepository()
            : base(ConfigurationManager.ConnectionStrings["YmatouUserConnectionString"].ConnectionString
                , "YmtRelease"
                ,
                (str, siBindThreadContext) =>
                    new _GeneralDbContext(() => new ConnectionContext(new SqlConnection(str))).Context(
                        siBindThreadContext))
        {

        }

        public int TryFindUserType(int userId)
        {
            return Utility.TryExecute(() =>
            {
                var sql = "select iType from Ymt_Users with(nolock) where iUserId=@UserID";
                var par = this.Parameters.AddInt("@UserId", userId).Build();
                using (var context = GetDbContext())
                    return context.Connection.Query<int>(sql, par, commandType: CommandType.Text,commandTimeout:3).FirstOrDefault();
            },int.MinValue, desc: "TryFindUserType");
        }
    }
}
